<template>
	<view class="backgroud">
		<view class="love_story">
			Love Story
		</view>
		<view class="inputcodetext">
			输入口令，链接心动的Ta~
		</view>
		<view class="codeline">
			<view class="inputcodeframe">
				<input type="text" v-model="input" placeholder="Ta一直在等你~" placeholder-class="tips">
			</view>
			<view class="createcode" @tap="createcode">
				创建口令
			</view>
		</view>
		<view class="love_button" @tap="linkaccount">
			<image src="../../static/恋接按钮爱心图标.png" mode="heightFix"></image>
		</view>
		<view class="boyandgirl">
			<image src="../../static/image.png" mode="heightFix"></image>
		</view>
	</view>
</template>

<script>
	const codeReg = /^[A-Z0-9]{6}$/;
	export default {
		onLoad(options) {
			this.id = uni.getStorageSync("id");
		},
		data() {
			return {
				id: "",
				cpId: "",
				code: "",
				input: ""
			}
		},
		methods: {
			createcode() {
				uni.request({
					header: {
						token: uni.getStorageSync("token")
					},
					data: {
						id: this.id
					},
					url: this.$baseUrl + "/user/createCode",
					method: 'POST',
					success: (res) => {
						//获取口令
						this.code = res.data.data
						uni.showModal({
							title: '你的口令为:',
							content: "" + this.code,
							success: (res) => {
								if (res.confirm) {
									uni.showModal({
										title: "匹配确认",
										content: "你的对象已经匹配了你的口令吗",
										success: (res) => {
											if (res.confirm) {
												//检查是否有账号与其配对
												uni.request({
													header: {
														token: uni
															.getStorageSync(
																"token")
													},
													data: {
														code: this.code,
														id: this.id
													},

													url: this.$baseUrl +
														"/user/isPaired",
													method: 'POST',
													success: (res) => {
														if (res.data
															.type ===
															'noPaired') {
															uni.showToast({
																title: '未匹配口令',
																icon: 'error',
																duration: 2000,
																success: (
																	res
																	) => {
																	// 重置口令
																	uni.request({
																		header: {
																			token: uni
																				.getStorageSync(
																					"token"
																					)
																		},
																		data: {
																			id: this
																				.id
																		},
																		url: this
																			.$baseUrl +
																			"/user/resetCode",
																		method: 'POST'
																	})
																}
															})
														} else {
															this.cpId = res
																.data.cpId,
																uni
																.setStorageSync(
																	'id',
																	this.id
																	),
																uni
																.setStorageSync(
																	'cpId',
																	this
																	.cpId),
																uni
																.showToast({
																	title: "匹配成功",
																	icon: 'success',
																	duration: 1500
																}),

																setTimeout(
																	() => {
																		uni.navigateTo({
																			url: "/pages/heartbeat/heartbeat"
																		})
																	}, 1500
																	)
														}
													}
												})
											} else {
												uni.request({
													header: {
														token: uni
															.getStorageSync(
																"token")
													},
													data: {
														code: this.code,
														id: this.id
													},
													url: this.$baseUrl +
														"/user/isPaired",
													method: 'POST',
													success: (res) => {
														if (res.data
															.type ===
															'noPaired') {
															uni.showToast({
																title: '未匹配口令',
																icon: 'error',
																duration: 2000,
																success: (
																	res
																	) => {
																	// 重置口令
																	uni.request({
																		header: {
																			token: uni
																				.getStorageSync(
																					"token"
																					)
																		},
																		data: {
																			id: this
																				.id
																		},
																		url: this
																			.$baseUrl +
																			"/user/resetCode",
																		method: 'POST'
																	})
																}
															})
														} else {
															this.cpId = res
																.data.cpId,
																uni
																.setStorageSync(
																	'id',
																	this.id
																	),
																uni
																.setStorageSync(
																	'cpId',
																	this
																	.cpId),
																uni
																.showToast({
																	title: "匹配成功",
																	icon: 'success',
																	duration: 1500
																}),
																setTimeout(
																	() => {
																		uni.navigateTo({
																			url: "/pages/heartbeat/heartbeat"
																		})
																	}, 1500
																	)
														}
													}
												})
											}
										}
									})
								} else {
									uni.request({
										header: {
											token: uni.getStorageSync("token")
										},
										data: {
											code: this.code,
											id: this.id
										},
										url: this.$baseUrl + "/user/isPaired",
										method: 'POST',
										success: (res) => {
											if (res.data.type === 'noPaired') {
												uni.showToast({
													title: '未匹配口令',
													icon: 'error',
													duration: 2000,
													success: (res) => {
														// 重置口令
														uni.request({
															header: {
																token: uni
																	.getStorageSync(
																		"token"
																		)
															},
															data: {
																id: this
																	.id
															},
															url: this
																.$baseUrl +
																"/user/resetCode",
															method: 'POST'
														})
													}
												})
											} else {
												this.cpId = res.data.cpId,
													uni.setStorageSync('id', this.id),
													uni.setStorageSync('cpId', this
														.cpId),

													uni.showToast({
														title: "匹配成功",
														icon: 'success',
														duration: 1500
													}),

													setTimeout(() => {
														uni.navigateTo({
															url: "/pages/heartbeat/heartbeat?"
														})
													}, 1500)
											}
										}
									})
								}
							}
						})
					}
				})
			},
			linkaccount() {
				if (codeReg.test(this.input)) {
					uni.request({
						header: {
							token: uni.getStorageSync("token")
						},
						data: {
							code: this.input,
							id: this.id
						},
						url: this.$baseUrl + "/user/linkCp",
						method: 'POST',
						success: (res) => {
							if (res.data.type === 'noPaired') {
								uni.showToast({
									title: '未匹配口令,请检查是否有误',
									icon: 'none',
									duration: 2000
								})
							} else {
								uni.showModal({
									title: "将与以下用户配对",
									content: "" + res.data.name,
									success: (res) => {
										if (res.confirm) {
											uni.request({
												header: {
													token: uni.getStorageSync("token")
												},
												data: {
													code: this.input,
													id: this.id
												},
												url: this.$baseUrl + "/user/linkCp2",
												method: 'POST',
												success: (res) => {
													this.cpId = res.data.cpId,
														uni.setStorageSync('id', this
															.id),
														uni.setStorageSync('cpId', this
															.cpId),
														uni.showToast({
															title: "匹配成功",
															icon: 'success',
															duration: 1500
														}),

														setTimeout(() => {
															uni.navigateTo({
																url: "/pages/heartbeat/heartbeat?"
															})
														}, 1500)
												}
											})
										}
									}
								})
							}
						}
					})
				} else {
					uni.showToast({
						title: "口令为6个大写字母或数字组成",
						icon: 'error',
						duration: 1500
					})
				}
			}
		}
	}
</script>

<style>
	.backgroud {
		background: linear-gradient(197.99deg, #2C9AFF -14.52%, #9C74F3 16.96%, #EC81B4 54.17%, #F1BC97 74.34%, #F4E283 100%);
		height: 100vh;
		width: 100vw;
	}

	.love_story {
		padding-top: 80px;
		margin-left: 24px;
		font-family: 'PingFang SC';
		font-style: normal;
		font-weight: 500;
		font-size: 36px;
		line-height: 36px;
		/* identical to box height, or 100% */

		color: #FFFFFF;
	}

	.inputcodetext {
		padding-left: 24px;
		font-family: 'PingFang SC';
		font-style: normal;
		font-weight: 500;
		font-size: 18px;
		line-height: 50px;

		color: #FFFFFF;
	}

	.inputcodeframe {
		width: 207px;
		height: 34px;
		margin-top: 5px;
		margin-left: 24px;
		padding-top: 10px;
		padding-left: 15px;

		background: #FFFFFF;
		mix-blend-mode: normal;
		opacity: 0.3;
		border-radius: 25px;
	}

	.inputcode {
		font-family: 'PingFang SC';
		font-style: normal;
		font-weight: 500;
		font-size: 18px;
		line-height: 25px;
		text-align: center;

		color: #FFFFFF;

		opacity: 0.6;
	}

	.love_button {
		margin-top: 18px;
		text-align: center;
		justify-content: center;
	}

	.boyandgirl {
		margin-top: 25px;
		text-align: center;
		backdrop-filter: blur(2.5px);
	}

	.tips {
		font-family: PingFang SC;
		font-size: 18px;
		font-weight: 500;
		line-height: 25px;
		letter-spacing: 0px;

	}

	.codeline {
		display: flex;
		flex-direction: row;
		justify-content: space-between;
		align-items: center;
	}

	.createcode {
		font-family: 'PingFang SC';
		font-style: normal;
		font-weight: 500;
		font-size: 18px;
		position: relative;
		top: 3px;
		line-height: 34px;
		width: auto;
		height: 34px;
		background-color: lightpink;
		opacity: 0.5;
		border-top-left-radius: 10px;
		border-bottom-left-radius: 10px;
		padding-left: 10px;
	}
</style>